/* Functionality for leftnav clock */
$(document).ready(function(){
	Clock.init();
	// jump start the clock
	clockInterval = setInterval(function(){
		Clock.run();
	},1000);
});
Clock = {
	init:function(){
		Clock.$hour = $('.jstime-hour');
		Clock.$minute = $('.jstime-minute');
		Clock.$second = $('.jstime-second');
		Clock.$ampm = $('.jstime-ampm');
		Clock.$weekday = $('.jstime-weekday');
		Clock.$month = $('.jstime-month');
		Clock.$date = $('.jstime-date');
		Clock.$year = $('.jstime-year');
		Clock.setTime();
		try{
		// stop and jumpstart the clock again (because if the clock object is reloaded the interval needs to be cleared)
			clearInterval(clockInterval);
			clockInterval = setInterval(function(){
				Clock.run();
			},1000);
		}catch(e){/**/}
	},
	setTime:function(yyyy,mm,dd,hh,nn,ss,ampm,weekday){ // 12 hrs only
		var yyyy = yyyy || Clock.$year.text();
		var mm = mm || Clock.$month.text();
		var dd = dd || Clock.$date.text();
		var hh = hh || Clock.$hour.text();
		var nn = nn || Clock.$minute.text();
		var ss = ss || Clock.$second.text();
		var weekday = weekday || Clock.$weekday.text();
		var ampm = ampm || Clock.$ampm.text();
		// convert to int
		Clock.year = +yyyy;
		Clock.month = +mm;
		Clock.date = +dd;
		Clock.hour = +hh;
		Clock.minute = +nn;
		Clock.second = +ss;
		Clock.weekday = weekday;
		Clock.ampm = ampm;
		// reflect to view
		Clock.$year.text(Clock.year);
		Clock.$month.text(Clock.month);
		Clock.$date.text(Clock.date);
		var viewMinute = Clock.minute;
		if(viewMinute < 10){viewMinute = '0' + viewMinute;}
		Clock.$minute.text(viewMinute);
		var viewSecond = Clock.second;
		if(viewSecond < 10){viewSecond = '0' + viewSecond;}
		Clock.$second.text(viewSecond);
		Clock.$weekday.text(Clock.weekday);
		var viewHour = Clock.hour;
		Clock.$hour.text(viewHour);
		Clock.$ampm.text(ampm);
		Clock.adjustLeapYear();
		$(document).trigger('clock.change');
	},
	addSecond:function(){
		Clock.second++;
		if(Clock.second == 60){
			Clock.second = 0;
			Clock.addMinute();
		}
		var second = Clock.second;
		if(second < 10){
			second = '0'+second;
		}
		Clock.$second.text(second);
	},
	addMinute:function(){
		Clock.minute++;
		if(Clock.minute == 60){
			Clock.minute = 0;
			Clock.addHour();
		}
		var minute = Clock.minute;
		if(minute < 10){
			minute = '0'+minute;
		}
		Clock.$minute.text(minute);
		setTimeout(function(){
			$(document).trigger('clock.change');
		},2000);
	},
	addHour:function(){
		Clock.hour++;
		if(Clock.hour == 12){
			Clock.toggleAMPM();
		}
		if(Clock.hour == 13){
			Clock.hour = 1;
		}
		Clock.$hour.text(Clock.hour);
	},
	toggleAMPM:function(){
		if(Clock.ampm == "AM"){
			Clock.ampm = "PM";
		} else {
			Clock.ampm = "AM";
			Clock.addDay();
		}
		Clock.$ampm.text(Clock.ampm);
	},
	addDay:function(){
		Clock.date++;
		Clock.cycleWeekday();
		if(Clock.date > Clock.daysInMonth[Clock.month]){
			Clock.date = 1;
			Clock.addMonth();
		}
		Clock.$date.text(Clock.date);
	},
	cycleWeekday:function(){
		for(var i = 0; i < Clock.weekdays.length; i++){
			if(Clock.weekday == Clock.weekdays[i]){
				var currentDay = i;
			}
		}
		currentDay++;
		if(currentDay == 7){
			currentDay = 0;
		}
		Clock.weekday = Clock.weekdays[currentDay];
		Clock.$weekday.text(Clock.weekday);
	},
	weekdays:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],
	daysInMonth:{1:31,2:28,3:31,4:30,5:31,6:30,7:31,8:31,9:30,10:31,11:30,12:31},
	adjustLeapYear:function(){
		if(Clock.year % 4 == 0){
			Clock.daysInMonth['2'] = 29;
		}
	},
	addMonth:function(){
		Clock.month++;
		if(Clock.month == 13){
			Clock.month = 1;
			Clock.addYear();
		}
		Clock.$month.text(Clock.month);
	},
	addYear:function(){
		Clock.year++;
		Clock.adjustLeapYear();
		Clock.$year.text(Clock.year);
	},
	run:function(){
		Clock.addSecond();
	},
	toString:function(){
		var month = Clock.month;
		var date = Clock.date;
		var year = Clock.year+2000;
		var hour = Clock.hour;
		var minute = Clock.minute;
		var second = Clock.second;
		if(Clock.ampm == 'AM'){
			if(hour == 12){hour = 0;}
		} else {
			if(hour != 12){
				hour += 12;
			}
		}
		if(month < 10){month = '0'+month;}
		if(date < 10){date = '0'+ date;}
		if(hour <10){hour = '0'+hour;}
		if(minute <10){minute = '0'+minute}
		if(second <10){second = '0'+second}
		return month+'/'+date+'/'+year+' '+hour+':'+minute+':'+second;
	}
};
/* dst detection */
Date.prototype.stdTimezoneOffset = function() {
    var jan = new Date(this.getFullYear(), 0, 1);
    var jul = new Date(this.getFullYear(), 6, 1);
    return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}

Date.prototype.dst = function() {
    return this.getTimezoneOffset() < this.stdTimezoneOffset();
}